package com.jinmdz.fmis.dao.admin.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 角色关联Provider
 *
 * @author LiCongLu
 * @date 2020-03-19 9:57
 */
public class RoleRelationProvider {

    /**
     * 按照角色主键查询角色关联帐号用户列表
     *
     * @param roleId 角色主键
     * @return
     * @author LiCongLu
     * @date 2020-03-19 10:05
     */
    public String listRoleUserByRoleId(@Param("roleId") Integer roleId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" role_user_relation.id as role_user_relation_id,role_user_relation.version ");
                builder.append(" ,job_no,full_name,user_gender_code,mobile_phone,other_phone,system_user.address");
                builder.append(" ,system_user.disable_state,system_user.sort,system_user.remark ");
                builder.append(" ,company.organization_name as company_name,department.organization_name as department_name");
                SELECT(builder.toString());
                FROM(" role_user_relation ");
                JOIN(" system_user on system_user.id = system_user_id ");
                LEFT_OUTER_JOIN(" organization_institution company on company.id = company_id ");
                LEFT_OUTER_JOIN(" organization_institution department on department.id = department_id ");

                WHERE(" role_user_relation.deleted = 0 and system_user.deleted = 0 ");
                WHERE(" role_user_relation.business_system_role_id = #{roleId}");

                ORDER_BY(" system_user.job_no ");
            }
        }.toString();
    }

    /**
     * 按照关联主键查询关联用户列表数据
     *
     * @param id 关联主键
     * @return
     * @author LiCongLu
     * @date 2020-03-19 11:04
     */
    public String getRoleUserById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" role_user_relation.id as role_user_relation_id,role_user_relation.version ");
                builder.append(" ,job_no,full_name,user_gender_code,mobile_phone,other_phone,system_user.address");
                builder.append(" ,system_user.disable_state,system_user.sort,system_user.remark ");
                builder.append(" ,company.organization_name as company_name,department.organization_name as department_name");
                SELECT(builder.toString());
                FROM(" role_user_relation ");
                JOIN(" system_user on system_user.id = system_user_id ");
                LEFT_OUTER_JOIN(" organization_institution company on company.id = company_id ");
                LEFT_OUTER_JOIN(" organization_institution department on department.id = department_id ");

                WHERE(" role_user_relation.deleted = 0 and system_user.deleted = 0 ");
                WHERE(" role_user_relation.id = #{id} ");
                ORDER_BY(" role_user_relation.id limit 1 ");
            }
        }.toString();
    }

    /**
     * 按照系统主键及角色主键查询菜单列表
     *
     * @param systemId 系统主键
     * @param roleId   角色主键
     * @return
     * @author LiCongLu
     * @date 2020-03-19 12:57
     */
    public String listRoleMenuListBySystemId(@Param("systemId") Integer systemId, @Param("roleId") Integer roleId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id,parent_id,menu_name ");
                builder.append(" ,( select count(*) from role_menu_relation ");
                builder.append("    where deleted = 0 ");
                builder.append("     and business_system_menu_id = business_system_menu.id");
                builder.append("     and business_system_role_id = #{roleId} ");
                builder.append("   ) as selected");
                SELECT(builder.toString());
                FROM(" business_system_menu ");
                WHERE(" deleted = 0 and disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} ");
                ORDER_BY(" parent_id,sort ");
            }
        }.toString();
    }

    /**
     * 按照系统主键及角色主键查询菜单按钮列表
     *
     * @param systemId 系统主键
     * @param roleId   角色主键
     * @return
     * @author LiCongLu
     * @date 2020-03-19 12:57
     */
    public String listRoleMenuButtonListBySystemId(@Param("systemId") Integer systemId, @Param("roleId") Integer roleId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" business_system_menu_button.id,business_system_menu_id,button_name,button_describe ");
                builder.append(" ,( select count(*) from role_menu_button_relation ");
                builder.append("    where deleted = 0 ");
                builder.append("     and business_system_menu_button_id = business_system_menu_button.id ");
                builder.append("     and business_system_menu_id = business_system_menu.id ");
                builder.append("     and business_system_role_id = #{roleId} ");
                builder.append("   ) as selected");
                SELECT(builder.toString());
                FROM(" business_system_menu_button ");
                JOIN(" business_system_menu on business_system_menu.id = business_system_menu_id ");
                WHERE(" business_system_menu_button.deleted = 0 and business_system_menu.deleted = 0");
                WHERE(" business_system_menu.disable_state = 0 and business_system_menu_button.disable_state = 0 ");
                WHERE(" business_system_id = #{systemId} ");
                ORDER_BY(" business_system_menu_button.sort ");
            }
        }.toString();
    }
}
